# -*- coding: utf-8 -*-
#

from service import Service
from sippeer.utils.logutil import logger


class ServicePool:
    """
    Running service pool
    The services in the pool are all started.
    It is used to find the running service for serviceFactory and
     if not found, it will create running service if possible.
     
     Also implemented using Singleton design pattern
    """
    
    class __ServicePool:
        """
        ServicePool's concrete class
        """
        
        def __init__(self):
            
            self._serviceRunningList = {}
        
        def getServiceByName(self, service_name):
            
            try:
                if service_name == None or service_name == "":
                    logger.error("ServicePool::getServiceByName(): service name is NULL!")
                    return None
                logger.debug("ServicePool::getServiceByName(): return service instance by service name " + service_name)
                return self._serviceRunningList[service_name]
            
            except KeyError:
                logger.error("ServicePool::getServiceByName(): service name got KeyError")
                return None
        
            
        def addRunningServive(self, service):
            """
            @param service:  Service class instance, type(service) == Service
            """
            
            if service == None:
                logger.error("ServicePool::addRunningService(): running service can not be None!")
                return
            
            self._serviceRunningList[service.getServiceName()] = service
            logger.debug("ServicePool::addRunningService(): add service in running pool by service name " + service.getServiceName())
            
        def removeRunningService(self, sevice_name):
            
            try:
                if service_name == None or service_name == "":
                    logger.error("ServicePool::removeRunningService(): service name is NULL")
                    return
                del self._serviceRunningList[service_name]
                logger.debug("ServicePool::removeRunningService(): remove running service by service name " + service_name)
                
            except KeyError:
                logger.error("ServicePool::removeRunningService(): service name got KeyError!")
                return
            
        def findRunningService(self, service_name):
            
            try:
                if service_name == None or service_name == "":
                    logger.error("ServicePool::findRunningService(): service name is NULL")
                    return False
                if self._serviceRunningList[service_name] != None:
                    logger.debug("ServicePool::findRunningService(): find service by service name: " + service_name)
                    return True
                else:
                    logger.debug("ServicePool::findRunningService(): Can not find service by service name: " + service_name)
                    return False
                
            except KeyError:
                logger.error("ServicePool::findRunningService(): service name got KeyError!")
                return False
            
    
    __instance = None
    
    def getInstance():
        """
        Singleton design
        """
        if ServicePool.__instance == None:
            ServicePool.__instance = ServicePool.__ServicePool()
        return ServicePool.__instance
    getInstance = staticmethod(getInstance)
                    
                
                
                    
                
            
    
    